package io.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        //阻塞方法
        Socket clientSocket = serverSocket.accept();
        System.out.println("有客户端连接");
       new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   handler(clientSocket);
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }).start();
    }

    private static void handler(Socket clientSocket) throws IOException {
        byte[] bytes = new byte[1024];
        System.out.println("ready...");
        //接收方法
        int read = clientSocket.getInputStream().read(bytes);
        if (read != -1) {
            System.err.println("接收到客户端的数据：" + new String(bytes, 0, read));
        }

        clientSocket.getOutputStream().write("HelloClient".getBytes());
        clientSocket.getOutputStream().flush();
    }

}
